import java.io.File;
import java.io.FilenameFilter;
import java.io.PrintWriter;
import java.util.Arrays;

public class GoSpam {
	static String PATH_INPUT = "../spam";
	static File[] listOfFiles;
	static String PATH_GOSPAM = "../spam/gospam.sh";
	static PrintWriter pw;

	static String HEAD = "prun -np 1 -t 08:15:00 java -Xms10G -Xmx22G ";
	static String ALG = "weka.classifiers.trees.REPTree "
			+ "-M 2 -V 0.0010 -N 3 -S 1 -L 6";
	static String TRAX = " -t ";
	// filename
	static String TRAZ = " -no-cv ";
	static String EXT = "txt";
	static String TAIL = "/dev/null ";

	public static void main(String[] args) throws Exception {
		makeFoldersResAndModel();
		readSpam_ArffFiles();
		makePrintWriter();
		printCommands();
		closePrintWriter();
	}

	private static void closePrintWriter() {
		pw.close();
	}

	private static void makePrintWriter() throws Exception {
		pw = new PrintWriter(PATH_GOSPAM);
	}

	static void printCommands() throws Exception {
	    int counter = 0;
		for (int i = 0; i < listOfFiles.length; i++) {
			print(HEAD);
			print(ALG);
			print(TRAX);
			print(listOfFiles[i].getName());
			print(TRAZ);
			print(" > ");
			print("res/");
			print(listOfFiles[i].getName().replace(".arff", ""));
			print(".");
			print(EXT);
			print(" < ");
			print(TAIL);
			print(" &\n");

			if (i % 48 == 0 && i > 98) {
				closePrintWriter();
				PATH_GOSPAM += counter;
				counter++;
				makePrintWriter();
			}
		}
	}

	static void readSpam_ArffFiles() {
		File folder = new File(PATH_INPUT);
		listOfFiles = folder.listFiles(new MyFilter());
		Arrays.sort(listOfFiles);
	}

	static void makeFoldersResAndModel() {
		File res = new File(PATH_INPUT + "/res/");
		if (!res.exists()) {
			res.mkdirs();
			File model = new File(PATH_INPUT + "/model/");
			model.mkdirs();
		}
	}

	static void print(String s) {
		System.out.print(s);
		pw.print(s);
	}

}

class MyFilter implements FilenameFilter {
	@Override
	public boolean accept(File arg0, String arg1) {
		if (arg1.contains(".arff")) {
			return true;
		}
		return false;
	}
}
